﻿using System;
using System.Configuration;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
using KZ.Utilities;

namespace KZ.Lottery
{
    public class Global
    {
        public static int NumberOfValue = 78;
        public static int NumberOfGroup = 30;

        public static CultureInfo Culture;

        public static void OpenTab(Form frm)
        {
            string name = frm.Name;
            frm.TopLevel = false;
            frm.Dock = DockStyle.Fill;

            FrmMain frmMain = Application.OpenForms["FrmMain"] as FrmMain;
            Panel pnlMain = frmMain.Controls["pnlMain"] as Panel;
            TabControl tabCtrl = pnlMain.Controls["tbcMain"] as TabControl;
            if (!tabCtrl.TabPages.ContainsKey(name))
            {
                TabPage tabPage = new TabPage
                {
                    Name = frm.Name,
                    Text = frm.Text,
                };
                tabPage.Controls.Add(frm);
                tabCtrl.TabPages.Add(tabPage);

                frm.Show();
            }
            else
            {
                frm.Dispose();
            }

            tabCtrl.SelectedTab = tabCtrl.TabPages[name];
            frmMain.TabChanged();
        }

        public static void Clear(Control ctrl)
        {
            int number = ctrl.Controls.Count;
            for (int i = 0; i < number; i++)
            {
                if (ctrl.Controls[i].HasChildren) { Clear(ctrl.Controls[i]); }
                if (ctrl.Controls[i] is TextBox) { (ctrl.Controls[i] as TextBox).Text = String.Empty; }
                if (ctrl.Controls[i] is ComboBox) { (ctrl.Controls[i] as ComboBox).SelectedIndex = 0; }
                if (ctrl.Controls[i] is DataGridView) { (ctrl.Controls[i] as DataGridView).Rows.Clear(); }
            }
        }

        public static string GetDtgValue(DataGridView dtg, int colIndex)
        {
            string id = String.Empty;
            foreach (DataGridViewRow row in dtg.SelectedRows)
            {
                id += "'" + Utility.StringParse(row.Cells[colIndex].Value) + "',";
            }
            if (id.EndsWith(",")) { id = id.Remove(id.Length - 1); }

            return id;
        }

        public static string GetDtgValue(DataGridView dtg, int colIndex, int colConditionIndex)
        {
            string id = String.Empty;
            foreach (DataGridViewRow row in dtg.Rows)
            {
                if (Utility.BoolParse(row.Cells[colConditionIndex].Value))
                {
                    id += "'" + Utility.StringParse(row.Cells[colIndex].Value) + "',";
                }
            }
            if (id.EndsWith(",")) { id = id.Remove(id.Length - 1); }

            return id;
        }

        public static void SetMessage(ToolStripLabel lbl, string message, bool isSuccess)
        {
            lbl.Text = message;
            lbl.ForeColor = isSuccess ? Color.Blue : Color.Red;
            lbl.Visible = true;
        }

        public static void WriteConfig(string str)
        {
            //// open App.Config of executable
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            //// set the new values
            config.ConnectionStrings.ConnectionStrings["Default"].ConnectionString = str;

            //// save and refresh the config file
            config.Save(ConfigurationSaveMode.Minimal);
            ConfigurationManager.RefreshSection("connectionStrings");
        }
    }
}